<!--
  @author: cqz
  @desc: 通用表信息预览组件 包含表基本信息 字段信息 数据信息
  @date: 2022-04-22
-->
<template>
  <el-tabs v-model="activeName">
    <el-tab-pane label="表信息" name="first">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" class="custom-form"
               label-width="100px">
        <el-form-item label="表名称" prop="tableName" >
          <el-input v-model="dataForm.tableName" disabled></el-input>
        </el-form-item>
        <el-form-item label="数据源:" prop="srcName">
          <el-select v-model="dataForm.srcId" placeholder="请选择" style="width: 80%;margin-right: 20px;" disabled  @change="onSelectChange">
            <el-option v-for="src in srcList" :key="src.id" :label="src.name"  :value="src.id">
            </el-option>
          </el-select>
          <i class="el-icon-link" @click="testConnect" title="连接测试" style="font-size: 20px;padding: 10px"></i>
        </el-form-item>
        <el-form-item label="表代码" prop="type">
          <el-input v-model="dataForm.tableCode" disabled></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="dataForm.remark" disabled></el-input>
        </el-form-item>
        <extend  ref="extend" @refreshDataList="extendCallBack"></extend>
      </el-form>
    </el-tab-pane>
    <el-tab-pane label="字段信息" name="second">
      <div style="text-align:right;padding-bottom: 5px;">
        <el-input v-model="searchName" class="input-with-select" placeholder="字段名称或字段代码" clearable maxlength="20" style="width: 350px;" size="small" @clear="queryByName" @keyup.enter.native="queryByName">
          <el-button slot="append" @click="queryByName()" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <el-table v-loading="loading"
                :data="fieldList" :key="keyRefresh"
                border :height="mheight + 60"
                highlight-current-row
                :rules="dataScope"
                size="mini">
        <el-table-column
          type="index"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="fieldName"
          header-align="center"
          width="280"
          label="字段名">
          <template slot-scope="scope">{{scope.row.fieldName}}
          </template>
        </el-table-column>
        <el-table-column
          prop="fieldCode"
          header-align="center"
          width="200"
          label="字段代码">
          <template slot-scope="scope">{{scope.row.fieldCode}}
          </template>
        </el-table-column>
        <el-table-column
          prop="fieldType"
          header-align="center"
          align="center"
          width="200"
          label="字段类型">
          <template slot-scope="scope">{{scope.row.fieldType}}
          </template>
        </el-table-column>
        <el-table-column
          prop="length"
          header-align="center"
          align="center"
          label="字段长度">
          <template slot-scope="scope">{{scope.row.length}}
          </template>
        </el-table-column>
        <el-table-column
          prop="decimal"
          header-align="center"
          align="center"
          label="精度">
          <template slot-scope="scope">{{scope.row.fieldDecimal}}
          </template>
        </el-table-column>
        <el-table-column
          prop="isPk"
          header-align="center"
          align="center"
          label="是否主键">
          <template slot-scope="scope">
            <span v-if="scope.row.isPk === '1'">是</span>
            <span v-if="scope.row.isPk === '0'">否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="nullable"
          header-align="center"
          align="center"
          label="允许为空">
          <template slot-scope="scope">
            <span v-if="scope.row.nullable === '1'">是</span>
            <span v-if="scope.row.nullable === '0'">否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="dafaultValue"
          header-align="center"
          align="center"
          label="默认值">
          <template slot-scope="scope">{{scope.row.dafaultValue}}
          </template>
        </el-table-column>
      </el-table>
    </el-tab-pane>
    <el-tab-pane label="数据信息" name="fourth">
      <sql-operate :columns="columns" @searchClick="searchHandler" v-model="operate"  ref="sqlOpt" style="margin-bottom: 10px; "></sql-operate>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        :height="mheight">
        <el-table-column v-for="column in columns" :key="column" align="center" :prop="column.prop" :label="column.label" :min-width="columnWidth" ></el-table-column>
      </el-table>
      <el-pagination
        style="float: right;margin-top: 10px;margin-bottom: 10px;"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
  import Extend from '../model/extend'
  import SqlOperate from '../component/sql-operate-full'

  export default {
    props: {
      mheight: {
        type: String
      }
    },
    components: {
      Extend,
      SqlOperate
    },
    data () {
      return {
        dataList: [],
        dataListLoading: false,
        loading: false,
        activeName: 'first',
        srcList: [],
        fieldList: [],
        columns: [],
        fkList: [],
        dataForm: {
          id: '',
          tableName: '',
          tableType: '',
          srcId: '',
          remark: '',
          tableCode: '',
          status: 1,
          parentList: [],
          parentName: ''
        },
        dataRule: {
        },
        dataScope: {
        },
        columnWidth: '100',
        tempFields: [],
        fieldTypeList: [],
        searchName: '',
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0,
        key: '',
        operate: '',
        value: ''
      }
    },
    methods: {
      /***
       *
       * @param tableId 表id
       */
      init (tableId) {
        this.key = ''
        this.operate = ''
        this.value = ''
        this.columns = []
        this.pageIndex = 1
        this.dataForm.id = tableId || 0
        this.fieldList = []
        this.tempFields = []
        this.fieldTypeList = []
        this.searchName = ''
        this.dataForm.tableId = tableId || ''
        this.getDataList()
        this.$nextTick(() => {
          this.$refs.sqlOpt.clear()
          this.$refs['dataForm'].resetFields()
          this.dataForm.tableCode = ''
          this.dataForm.srcId = ''
          if (this.dataForm.id) {
            this.loading = true
            this.$http({
              url: this.$http.adornUrl(`/src/table/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tableName = data.table.tableName
                this.dataForm.tableCode = data.table.tableCode
                this.dataForm.srcId = data.table.datasourceId
                this.dataForm.remark = data.table.remark
                this.dataForm.tableType = data.table.tableType
                this.dataForm.status = data.table.status
                this.tempFields = data.table.fieldList === null ? [] : data.table.fieldList
                this.fillDataList(1)
                this.fieldTypeList = data.fieldTypeList === null ? [] : data.fieldTypeList
                this.pageIndex = 1
                this.fkList = data.fkList

                this.getDataSource()
              } else {
                this.$message.error(data.msg)
              }
              this.loading = false
            })
          }
        })
        this.$refs.extend.init(this.dataForm.id, '表')
      },
      getDataSource () {
        this.$http({
          url: this.$http.adornUrl(`/src/source/list`),
          method: 'get',
          params: this.$http.adornParams({
            'isCat': '0',
            'id': this.dataForm.srcId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.srcList = data.page.list
          } else {
            this.srcList = []
          }
        })
      },
      searchHandler (key, opt, keyVal) {
        this.key = key
        this.operate = opt
        this.value = keyVal
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        let whereStr = this.key + this.operate + this.value

        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/src/table/dataList/${this.dataForm.tableId}`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'where': whereStr,
            'opt': this.operate
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.columns = data.columns
            this.dataList = data.page.list
            this.totalCount = data.page.totalCount
          } else {
            this.dataList = []
            this.totalCount = 0
          }
          this.dataListLoading = false
        })
      },
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      /**
       * 字段查询
       */
      queryByName () {
        this.fieldList = []
        var name = this.searchName.toLowerCase()
        if (name === '') {
          this.pageIndex = 1
          this.fillDataList(1)
          return
        }
        for (var rowseq = 0; rowseq < this.tempFields.length; rowseq++) {
          var fieldName = this.tempFields[rowseq].fieldName.toLowerCase()
          var fieldCode = this.tempFields[rowseq].fieldCode.toLowerCase()
          if (fieldName.indexOf(name) >= 0 || fieldCode.indexOf(name) >= 0) {
            this.fieldList.push(this.tempFields[rowseq])
          }
        }
      },
      /**
       * 在详情预览的时候，该方法直接把字段列表全部显示了
       * @param val
       */
      fillDataList (val) {
        for (var i = 0; i < this.tempFields.length; i++) {
          this.tempFields[i].isPk = this.tempFields[i].isPk.toString()
          this.tempFields[i].nullable = this.tempFields[i].nullable.toString()
          this.fieldList.push(this.tempFields[i])
        }
      },
      /**
       * 数据源测试
       * @id
       */
      testConnect () {
        this.$http({
          url: this.$http.adornUrl('/src/source/connecting/' + this.dataForm.srcId),
          method: 'get'
        }).then(({data}) => {
          if (data && data.result === true) {
            this.$message({
              message: '测试连接成功',
              type: 'success',
              duration: 1500
            })
          } else {
            this.$message.error('测试连接失败')
          }
        })
      },
      onSelectChange (id) {
        this.dataForm.srcId = id
      }
    }
  }
</script>
<style>
  .custom-form .el-select .el-input.is-disabled .el-input__inner,
  .custom-form .el-input.is-disabled .el-input__inner {
    background-color: #FFF;
    cursor: default;
    color: #606266;
  }
</style>
